//user 对象
var User = {
	id:"userTable",		//表格id
	seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    //初始化表格的列
    initColumn:function () {
        var columns = [
            {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '序号', field: '', align: 'center', valign: 'middle', sortable: false,width:'50px',
            	formatter:function(value,row,index){
            		return index+1;
            	}
            },
            {title: '账号', field: 'account', align: 'center', valign: 'middle', sortable: true},
            {title: '姓名', field: 'userName', align: 'center', valign: 'middle', sortable: true},
            {title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true,
            	formatter:function(value,row,index){
            		if(value==1){
            			return '男';
            		}
            		return '女';
            	}
            },
          //  {title: '角色', field: 'roleList', align: 'center', valign: 'middle', sortable: true},
           // {title: '部门', field: 'sysGroup', align: 'center', valign: 'middle', sortable: true},
            {title: '电话', field: 'phone', align: 'center', valign: 'middle', sortable: true},
            {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true,
            	formatter:function(value,row,index){
            		var date = new Date(value);
            		return Base.dateFomat(date);
            }},
            {title: '状态', field: 'status', align: 'center', valign: 'middle', sortable: true,
            	formatter:function(value,row,index){
            		if(value==1){
            			return '启用';
            		}
            		return '<span style="color:red">冻结</span>';
            	}
            }];
        return columns;
    },
    //检查是否选中
    check:function(){
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        if (selected.length == 0) {
            Base.info("请先选中表格中的某一记录！");
            return false;
        } else {
            User.seItem = selected[0];
            return true;
        }
    },
    //添加窗口
    addWindow:function () {
        var index = layer.open({
            type: 2,
            title: '添加用户',
            area: ['830px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Base.adminPath + '/sysUser/add'
        });
        this.layerIndex = index;
    },
    //修改窗口
    editWindow:function () {
        if (this.check()) {
            var index = layer.open({
                type: 2,
                title: '修改用户',
                area: ['800px', '450px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Base.adminPath + '/sysUser/edit/' + this.seItem.id
            });
            this.layerIndex = index;
        }
    },
    //删除
    doDelete:function () {
        if (this.check()) {
            var operation = function(){
                var ajax = new $ajax(Base.adminPath + "/sysUser/delete", function (data) {
                	if(data.status){
        				Base.success(data.data);
        				User.table.refresh(); // 刷新列表
        			}else{
        				Base.error(data.message);
        			}
                }, function (data) {
                    Base.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", User.seItem.id);
                ajax.start();
            };

            Base.confirm("是否刪除该用户?", operation);
        }
    },
    doSearch:function () {
        var queryData = {};

        queryData['account'] = $("#account").val();
        queryData['beginTime'] = $("#beginTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['groupId'] = User.groupId;

        User.table.refresh({query: queryData});
    },
    resetSearch:function(){
    	$("#account").val("");
        $("#beginTime").val("");
        $("#endTime").val("");

        User.doSearch();
    },
    clickGroupTree:function(e, treeId, treeNode){
    	User.groupId = treeNode.id;
        User.doSearch();
    }
}


$(function () {
	layui.use(['layer','laydate'], function(){
	  var layer = layui.layer;
	  var laydate = layui.laydate;
	  
	  laydate.render({
		  elem: '#beginTime', //指定元素
		  format:'yyyy-MM-dd'
	  });
	  laydate.render({
		  elem: '#endTime', //指定元素
		  format:'yyyy-MM-dd'
	  });
	});
    var defaultColunms = User.initColumn();
    var table = new BSTable(User.id, Base.adminPath + "/sysUser/list", defaultColunms);
    table.setPaginationType('server');
    table.init();
    User.table = table;
    
    var ztree = new $ZTree("groupTree", Base.adminPath + "/sysGroup/ztreeList");
    ztree.bindOnClick(User.clickGroupTree);
    ztree.init();
    
});